﻿using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryDoctor : ADONetRepositoryBase<Doctor> 
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Doctor entity)
        {
            var cmd = new SqlCommand("DoctorInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Specialisation", entity.Specialisation));
            cmd.Parameters.Add(new SqlParameter("Category", entity.Category));
            cmd.Parameters.Add(new SqlParameter("EmployeeId", entity.Employee.Id));
            cmd.Parameters.Add(new SqlParameter("ScheduleId", entity.Schedule.Id));
            cmd.Parameters.Add(new SqlParameter("CabinetId", entity.Cabinet.Id));
            logger.Trace("SqlCommand для записи данных Доктора в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("DoctorDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Доктора id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Doctor entity)
        {
            var cmd = new SqlCommand("DoctorUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Specialisation", entity.Specialisation));
            cmd.Parameters.Add(new SqlParameter("Category", entity.Category));
            cmd.Parameters.Add(new SqlParameter("EmployeeId", entity.Employee.Id));
            cmd.Parameters.Add(new SqlParameter("ScheduleId", entity.Schedule.Id));
            cmd.Parameters.Add(new SqlParameter("CabinetId", entity.Cabinet.Id));
            logger.Trace("SqlCommand для обновления данных Доктора id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetDoctor", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Доктора id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            var cmd = new SqlCommand("GetDoctorList", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            logger.Trace("SqlCommand для выборки всех существующих Докторов сформирован");
            return cmd;
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetDoctorByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Доктора {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Doctor GetEntity(SqlDataReader sqlDataReader)
        {
            Doctor doctor = new Doctor();
            IRepository<Cabinet> adoNetRepositoryCabinet = new ADONetRepositoryCabinet();
            IRepository<Schedule> adoNetRepositorySchedule = new ADONetRepositorySchedule();
            IRepository<Employee> adoNetRepositoryEmployee = new ADONetRepositoryEmployee();
            doctor.Id = (int)sqlDataReader["Id"];
            doctor.Specialisation = sqlDataReader["Specialisation"].ToString();
            doctor.Category = sqlDataReader["Category"].ToString();
            doctor.Employee = adoNetRepositoryEmployee.GetById((int)sqlDataReader["EmployeeId"]);
            doctor.Schedule = adoNetRepositorySchedule.GetById((int)sqlDataReader["ScheduleId"]);
            doctor.Cabinet = adoNetRepositoryCabinet.GetById((int)sqlDataReader["CabinetId"]);
            return doctor;
        }
    }
}
